package com.ln.cmsln.service.impl;

import com.ln.cmsln.pojo.entity.Photo;
import com.ln.cmsln.pojo.entity.Relation;
import com.ln.cmsln.mapper.RelationMapper;
import com.ln.cmsln.service.RelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuwenzhe
 * @since 2022-03-31
 */
@Service
public class RelationServiceImpl extends ServiceImpl<RelationMapper, Relation> implements RelationService {

    @Override
    public boolean add(Relation relation, String realPath, MultipartFile file) {
        try {
            //按照时间分组存储图片
            SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd/HH/mm");
            String format = df.format(new Date());
            //组装图片存储的路径
            String imagesUrl = "\\src\\main\\resources\\image\\"+format+"\\"+file.getOriginalFilename();
            //绝对路径
            realPath=realPath+imagesUrl;
            File file2 = new File(realPath);

            //没有该路径则创建
            if (!file2.exists()){
                file2.mkdirs();
            }
            //写入文件
            file.transferTo(file2);
            //修改图片路径 这里放绝对路径还是相对路径，看自己
            relation.setRname(imagesUrl);
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return  baseMapper.insert(relation)>0;
    }

    @Override
    public boolean update(Relation relation, String realPath, MultipartFile file) {
        if (relation!=null){
            //查询页面信息
            Relation relation1 = baseMapper.selectById(relation.getRid());
            String imgUrl = relation1.getRname();
            File file1 = new File(realPath+imgUrl);
            file1.delete();
            try {
                //按照时间分组存储图片
                SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd/HH/mm");
                String format = df.format(new Date());
                //组装图片存储的路径
                String imagesUrl = "\\src\\main\\resources\\image\\"+format+"\\"+file.getOriginalFilename();
                realPath=realPath+imagesUrl;
                File file2 = new File(realPath);

                //没有该路径则创建
                if (!file2.exists()){
                    file2.mkdirs();
                }
                //写入文件
                file.transferTo(file2);
                //修改图片路径
                relation.setRname(imagesUrl);
            } catch (Exception e){
                e.printStackTrace();
                return false;
            }
        }
        return baseMapper.updateById(relation) > 0;
    }

    @Override
    public boolean deleteById(Long rid, String realPath) {
        //查询页面信息
        Relation relation = baseMapper.selectById(rid);
        if (relation!=null){

            String imgUrl = relation.getRname();
            File file1 = new File(realPath+imgUrl);
            file1.delete();

        }
        return  baseMapper.deleteById(relation)>0;
    }
}
